/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.io.singulart.particlesviewsample.slice;

import com.io.singulart.particlesviewsample.ResourceTable;
import com.io.singulart.particlesviewsample.Utils;
import com.onexeor.particlesview.ParticlesView;

import io.singulart.bottomnavigationbar.BottomNavigationBar;
import io.singulart.bottomnavigationbar.CenterNavigationButton;
import io.singulart.bottomnavigationbar.VectorPixelMap;
import io.singulart.particle_progress_bar.ParticleProgressBar;
import io.singulart.pulse_button.PulseButton;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;

import java.io.IOException;

/**
 * Main AbilitySlice
 */
public class MainAbilitySlice extends AbilitySlice {
    private PulseButton mPulseButton;
    private CenterNavigationButton mCBtn;
    private boolean mIsShow;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initPulseBtn();
        initProgress();
        initCenterBtn();
        initBottomNa();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initPulseBtn() {
        Component component = findComponentById(ResourceTable.Id_pulseBtn);
        if (!(component instanceof PulseButton)) {
            return;
        }
        mPulseButton = (PulseButton) component;
        mPulseButton.setPbColorCenterCircleStroke(Color.getIntColor("#21D9CC"));
        mPulseButton.setPbInnerCircleStrokePadding(14f);
        mPulseButton.setPbPulseSpeed(200);
        mPulseButton.setPbWavePadding(50);
        mPulseButton.setPbInnerCircleStrokeWidth(3);
        mPulseButton.setPbOuterCircleStrokeWidth(6);
        mPulseButton.setBmIcon(Utils.getPixelMap(getContext(), ResourceTable.Media_icon).get());
    }


    private void initProgress() {
        Component componentById = findComponentById(ResourceTable.Id_progress);
        if (!(componentById instanceof ParticleProgressBar)) {
            return;
        }
        ParticleProgressBar progressBar = (ParticleProgressBar) componentById;
        mPulseButton.setClickedListener(
                component -> {
                    if (mIsShow) {
                        mIsShow = false;
                        progressBar.disMissProgress();
                    } else {
                        mIsShow = true;
                        progressBar.showProgress();
                    }
                });
    }

    /**
     * app:cnb_background_color="#282B43"
     * app:cnb_foreground_color="#21D9CC"
     * app:cnb_foreground_color_normal="#666DA5"
     */
    private void initCenterBtn() {
        Component componentById = findComponentById(ResourceTable.Id_centerBtn);
        if (!(componentById instanceof CenterNavigationButton)) {
            return;
        }
        mCBtn = (CenterNavigationButton) componentById;
        mCBtn.setBackAndForeColor(Color.getIntColor("#282B43"),
                Color.getIntColor("#21D9CC"), Color.getIntColor("#666DA5"));
    }

    private void initBottomNa() {
        Component componentById = findComponentById(ResourceTable.Id_btn_ng);
        if (!(componentById instanceof BottomNavigationBar)) {
            return;
        }
        BottomNavigationBar navigationBar = (BottomNavigationBar) componentById;
        navigationBar.setCenterNavigationButton(mCBtn);
        if (mCBtn != null) {
            mCBtn.setBottomNavigationBar(navigationBar);
        }
        navigationBar.setOnItemSelectedListener(this::showDialog);
        VectorElement ff = new VectorElement(getContext(), ResourceTable.Graphic_ic_item_games);
        VectorElement ss = new VectorElement(getContext(), ResourceTable.Graphic_ic_item_social);
        VectorElement tt = new VectorElement(getContext(), ResourceTable.Graphic_ic_item_stats);
        VectorElement fo = new VectorElement(getContext(), ResourceTable.Graphic_ic_item_more);

        VectorElement fp = new VectorElement(getContext(), ResourceTable.Graphic_ic_item_games_press);
        VectorElement sp = new VectorElement(getContext(), ResourceTable.Graphic_ic_item_social_press);
        VectorElement tp = new VectorElement(getContext(), ResourceTable.Graphic_ic_item_stats_press);
        VectorElement fop = new VectorElement(getContext(), ResourceTable.Graphic_ic_item_more_press);

        try {
            float fp36 = getResourceManager().getElement(ResourceTable.Float_fp_36).getFloat();
            float fp25 = getResourceManager().getElement(ResourceTable.Float_fp_25).getFloat();
            float fp24 = getResourceManager().getElement(ResourceTable.Float_fp_24).getFloat();
            float fp22 = getResourceManager().getElement(ResourceTable.Float_fp_22).getFloat();
            float fp27 = getResourceManager().getElement(ResourceTable.Float_fp_27).getFloat();
            float fp7 = getResourceManager().getElement(ResourceTable.Float_fp_7).getFloat();

            navigationBar.setVectorPixelMap(
                    new VectorPixelMap(new VectorElement[]{ff, fp}, (int) fp36, (int) fp25),
                    new VectorPixelMap(new VectorElement[]{ss, sp}, (int) fp24, (int) fp24),
                    new VectorPixelMap(new VectorElement[]{tt, tp}, (int) fp22, (int) fp24),
                    new VectorPixelMap(new VectorElement[]{fo, fop}, (int) fp27, (int) fp7));
        } catch (IOException | NotExistException | WrongTypeException e) {
            HiLog.info(ParticlesView.sHiLogLabel, "csddd e:" + navigationBar);
        }
    }

    private void showDialog(int ps) {
        ToastDialog dialog = new ToastDialog(getAbility());
        dialog.setDuration(500);
        dialog.setContentText(String.valueOf(ps));
        dialog.setAlignment(LayoutAlignment.CENTER);
        dialog.show();
    }
}
